import { defineComponent, reactive, toRefs } from 'vue'
import { NInput, NForm, NFormItem, NModal } from 'naive-ui'
import { Response } from '@/service/modules/common/types'
import { updateName } from '@/service/modules/worker-groups'
import { useI18n } from 'vue-i18n'

const ModNameModal = defineComponent({
  name: 'ModProjectAliasModal',
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    workerId: {
      type: Number,
      required: true
    },
    name:{
      type: String
    }
  },
  emits: ['modalShowChange', 'refreshList'],
  setup() {
    const state = reactive({
      form: {
        workerId: 0,
        name: ''
      },
      positiveButtonDisabled: false
    })

    return { ...toRefs(state) }
  },
  methods: {
    async onPositiveClick() {
      //提交修改项目别名请求
      this.positiveButtonDisabled = true
      this.form.workerId = this.workerId
      const res: Response<any> = await updateName({
        ...this.form
      }).catch(() => {
        this.positiveButtonDisabled = false
      })
      if (res.resp_code === 0) {
        this.initData()
        this.$emit('modalShowChange', false)
        this.$emit('refreshList', false)
        this.positiveButtonDisabled = false
      } else {
        this.positiveButtonDisabled = false
      }
    },
    onNegativeClick() {
      this.initData()
      this.$emit('modalShowChange', false)
    },
    initData() {
      this.form.name = ''
    }
  },
  render() {
    const { t } = useI18n()
    return (
      <NModal
        show={this.showModal}
        mask-closable={false}
        style='width: 400px'
        title={ t('project.master.editnodename')}
        bordered={false}
        size='huge'
        preset='dialog'
        positive-text={ t('dialog.positiveText')}
        negative-text={ t('dialog.negativeText')}
        onPositiveClick={this.onPositiveClick}
        onNegativeClick={this.onNegativeClick}
        positiveButtonProps={{
          disabled: this.positiveButtonDisabled
        }}
        onClose={this.onNegativeClick}
      >
        <NForm>
          <NFormItem path='alias' label={ t('project.master.nodeName')}>
            <NInput
              type='text'
              size='large'
              v-model={[this.form.name, 'value']}
              autofocus
            />
          </NFormItem>
        </NForm>
      </NModal>
    )
  }
})

export default ModNameModal
